package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.entity.UserExt;
import com.guoguo.entity.Users;
import com.guoguo.entity.vo.UsersVO;
import com.guoguo.service.IUserExtService;
import com.guoguo.service.IUsersService;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/7 0007.
 */
@Controller
@RequestMapping("/view")
public class UserController extends BaseController {

    @Resource
    private IUserExtService iuserExtService;
    @Resource
    private IUsersService iusersService;

    /**
     * 查询所有用户
     *
     * @param
     * @return
     */

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public ModelAndView user(@RequestParam int pageNumber, @RequestParam int pageSize) {
        Map<String, Object> data = new HashedMap();
        PageBean<UsersVO> userList = iusersService.selectUserList(pageNumber, pageSize);
        data.put("userList", userList);
        return returnView("/user", data);
    }

    @ResponseBody
    @RequestMapping(value = "/judge", method = RequestMethod.POST)
    public Integer userJudge(String username) {
        int mes = 0;
        int i = iusersService.count(username);
        if (i == 0) {
            mes = 0;
        } else {
            mes = 1;
        }
        return mes;
    }

    /**
     * 添加用户
     */
    @ResponseBody
    @RequestMapping(value = "/saveUsers", method = RequestMethod.POST)
    public JSONObject saveUsers(String username, String userpwd, String nickname, String qq, String webchat, String weibo, String phone) {
        Users users = new Users();
        UserExt userExt = new UserExt();
        users.setUserId(UUIDUtils.getUUID());
        userExt.setUserId(users.getUserId());
        users.setUsername(username);
        users.setUserpwd(userpwd);
        users.setNickname(nickname);
        users.setQq(qq);
        users.setWebchat(webchat);
        users.setWeibo(weibo);
        users.setPhone(phone);
        Date now = new Date();
        int i = iusersService.insertUsers(users);
        userExt.setNoSpeaking(0);
        userExt.setPhone(users.getPhone());
        userExt.setCreateTime(now);
        userExt.setIsMember(false);
        userExt.setMemType(null);
        userExt.setBuyCount(0);
        userExt.setBuySurplus(0);
        userExt.setRegistered(now);
        userExt.setEmail(null);
        userExt.setMoney(null);

        iuserExtService.insertUserExt(userExt);
        Map<String, Object> data = new HashedMap();
        //System.out.println(statement1);
        data.put("userList", i);
        return returnResult(200, data, "");
    }

    /**
     * 去跳转修改页面
     */

    @RequestMapping(value = "/toUpdateUser", method = RequestMethod.GET)
    public ModelAndView User(String userId) {
        Map<String, Object> data = new HashedMap();
        Users users = iusersService.findUserById(userId);
        data.put("users", users);
        return returnView("/user/userUpdate", data);
    }

    /**
     * 修改用户信息
     */
    @ResponseBody
    @RequestMapping(value = "/updateUser", method = RequestMethod.GET)
    public JSONObject updateUsers(Users users) {
        Map<String, Object> data = new HashedMap();
        UserExt userExt = new UserExt();
        Date now = new Date();
        iusersService.updateUsers(users);
        userExt.setUserId(users.getUserId());
        userExt.setUpdateTime(now);
        userExt.setPhone(users.getPhone());
        iuserExtService.updateUserExt(userExt);
        return returnResult(200, data, "");
    }
}
